﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace BDSim
{
    /// <summary>
    /// CalSunMoonPos.xaml 的交互逻辑
    /// </summary>
    public partial class CalSunMoonPos : Window
    {
        public CalSunMoonPos()
        {
            InitializeComponent();
            comboBox1.Items.Add("太阳");
            comboBox1.Items.Add("月亮");
            comboBox1.SelectedIndex = 0;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            DateTime dt1 = dateTimePicker1.Value;
            DateTime dt2 = dateTimePicker2.Value;
            UTCTime start = UTCTime.FromDateTime(dt1);
            UTCTime end = UTCTime.FromDateTime(dt2);
            double sec = 0.0;

            // 判断输入的时间间隔是否合法
            try
            {
                sec = Convert.ToDouble(textBox2.Text);
            }
            catch (FormatException)
            {
                MessageBox.Show(String.Format("Unable to convert '{0}' to a Double.", textBox2.Text), "错误");
                return;
            }
            catch (OverflowException)
            {
                MessageBox.Show(String.Format("'{0}' is outside the range of a Double.", textBox2.Text), "错误");
                return;
            }

            // 判断输入的起止时间是否在有效范围内
            if ((start.CompareTo(BDSimObject.GlobalStartTime) < 0) ||
               (end.CompareTo(BDSimObject.GlobalEndTime) > 0))
            {
                MessageBox.Show("计算时间超出仿真时间范围，请重新输入", "提示");
                return;
            }

            double[] PosMoon = new double[6];
            double[] PosSun = new double[6];
            StringBuilder sun_show = new StringBuilder("太阳位置（ECI坐标）\r\n\r\n" +
                "        时间              x坐标（米）         y坐标（米）         z坐标（米）  \r\n" +
                "-------------------  ------------------  ------------------  ------------------\r\n");
            StringBuilder moon_show = new StringBuilder("月亮位置（ECI坐标）\r\n\r\n" +
                "        时间              x坐标（米）         y坐标（米）         z坐标（米）  \r\n" +
                "-------------------  ------------------  ------------------  ------------------\r\n");
            for (UTCTime time = start; time.CompareTo(end) <= 0; time.AddSecond(sec))
            {
                bool ret = BDSim.AstroMotionModel.CalSunMoonPos(time, PosMoon, PosSun);
                sun_show.Append(time.ToString() +
                "  " + PosSun[0].ToString().PadLeft(18, ' ') +
                "  " + PosSun[1].ToString().PadLeft(18, ' ') +
                "  " + PosSun[2].ToString().PadLeft(18, ' ') + "\r\n");
                moon_show.Append(time.ToString() +
                "  " + PosMoon[0].ToString().PadLeft(18, ' ') +
                "  " + PosMoon[1].ToString().PadLeft(18, ' ') +
                "  " + PosMoon[2].ToString().PadLeft(18, ' ') + "\r\n");
            }

            if (comboBox1.SelectedIndex == 0)
            {
                textBox1.Text = sun_show.ToString();
            }
            else
            {
                textBox1.Text = moon_show.ToString();
            }
        }
    }
}
